const nodemailer = require("nodemailer");

export function sendEmail(
  to: string,
  subject: string,
  text: string
): Promise<boolean> {
  const transporter = nodemailer.createTransport(
    `smtps://stephenykk_pan@163.com:${process.env.MAIL_AUTH_CODE}@smtp.163.com`
  );

  const fromEmail = "stephenykk_pan@163.com";
  // Compose the email
  const mailOptions = { from: fromEmail, to, subject, html: text };

  // Send the email
  return new Promise((resolve) => {
    transporter.sendMail(
      mailOptions,
      function (error: any, info: { response: any }) {
        if (error) {
          console.log("Email sent FAIL:", error);
          resolve(false);
        } else {
          console.log("Email sent OK:", info.response);
          resolve(true);
        }
      }
    );
  });
}
